﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Globalization;

namespace TibiaAutoHealer
{
    public partial class Form1 : Form
    {
        #region decalarações
        AutoCompleteStringCollection acsc;
        string appPath = System.IO.Directory.GetCurrentDirectory();
        bool tibiaAberto = false;
        #endregion

        public Form1()
        {
            InitializeComponent();

            acsc = new AutoCompleteStringCollection();

            txtSpell1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtSpell1.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtSpell1.AutoCompleteCustomSource = acsc;

            txtSpell2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtSpell2.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtSpell2.AutoCompleteCustomSource = acsc;

            try
            {
                Funcoes.desabilitarASLR();
            }
            catch { }
        }

        private void btnFechar_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

        private void btnEsconder_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.Visible = false;
            TrayIcon.Visible = true;
            cmsMostrar.Enabled = true;
            TrayIcon.ShowBalloonTip(10);
        }

        private void fecharToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnFechar_Click(sender, e);
        }

        private void cmsMostrar_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            TrayIcon.Visible = false;
            cmsMostrar.Enabled = false;
        }

        private void chkHP_Click(object sender, EventArgs e)
        {
            if (chkHP.Checked && cboHotkeyHP.Text != "" && cboHotkeyHP.Text != "-- Selecione --" && txtQtdeHP.Text != "")
            {
                tmrHP.Start();
            }
            else
            {
                tmrHP.Stop();
            }
        }

        private void chkMP_Click(object sender, EventArgs e)
        {
            if (chkMP.Checked && cboHotkeyMP.Text != "" && cboHotkeyMP.Text != "-- Selecione --" && txtQtdeMP.Text != "")
            {
                tmrMP.Start();
            }
            else
            {
                tmrMP.Stop();
            }
        }

        private void tmrHP_Tick(object sender, EventArgs e)
        {
            if (Funcoes.GetHP() < Convert.ToInt32(txtQtdeHP.Text))
            {
                Funcoes.Hotkey(cboHotkeyHP.Text);
            }
        }

        private void tmrMP_Tick(object sender, EventArgs e)
        {
            if (Funcoes.GetMana() < Convert.ToInt32(txtQtdeMP.Text))
            {
                Funcoes.Hotkey(cboHotkeyMP.Text);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Verifica processos
            System.Diagnostics.Process[] Processos;
            Processos = System.Diagnostics.Process.GetProcesses();

            for (int i = 0; i < Processos.Length; i++)
            {
                if (Processos[i].ProcessName == "Tibia")
                {
                    tibiaAberto = true;

                    //Checando a versão e adequando os endereços
                    if (Funcoes.VersaoTibia() == "9.5.3.0")
                    {
                        Versoes.Versao953.SetaVersao953();
                    }
                }
            }

            //tempo dos timers
            tmrHP.Interval = Convert.ToInt32(tempoHP.Value);
            tmrMP.Interval = Convert.ToInt32(tempoMP.Value);

            //Verifica se o tibia esta aberto
            if (!tibiaAberto)
            {
                MessageBox.Show("Você deve abrir o Tibia antes de executar o Tibia Auto Healer!", "Atenção:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
                this.Close();
            }
            else
            {
                this.Visible = true;
            }

            //Spells
            validaListaMagias();
            CarregaListaMagias();
            CarregaListaMagias_txtListaSpells();

            //info char
            tmrInfoChar.Start();
        }

        private void lnkInfo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (MessageBox.Show("Quer visitar o meu blog-site?", "Pergunta:", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                System.Diagnostics.Process.Start("http://jvprogramador.blogspot.com");
            }
        }

        private void tmrConfig_Tick(object sender, EventArgs e)
        {
            tmrHP.Interval = Convert.ToInt32(tempoHP.Value);
            tmrMP.Interval = Convert.ToInt32(tempoMP.Value);
            tmrHPPotion.Interval = Convert.ToInt32(tempoHPPotion.Value);
        }

        private void chkHPPotion_Click(object sender, EventArgs e)
        {
            if (chkHPPotion.Checked && cboHotkeyHPPotion.Text != "" && cboHotkeyHPPotion.Text != "-- Selecione --" && txtQtdeHPPotion.Text != "")
            {
                tmrHPPotion.Start();
            }
            else
            {
                tmrHPPotion.Stop();
            }
        }

        private void tmrHPPotion_Tick(object sender, EventArgs e)
        {
            if (Funcoes.GetHP() < Convert.ToInt32(txtQtdeHPPotion.Text))
            {
                Funcoes.Hotkey(cboHotkeyHPPotion.Text);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2 { StartPosition = FormStartPosition.CenterParent };
            frm2.ShowDialog(this);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            btnEsconder_Click(sender, e);
            e.Cancel = true;
        }

        private void Form1_HelpRequested(object sender, HelpEventArgs hlpevent)
        {
            button1_Click(sender, hlpevent);
        }

        #region funcoes
        //Função carregar lista de magias auto completar
        private void CarregaListaMagias()
        {
            try
            {
                StreamReader sr = new StreamReader(appPath + "\\lista_magias.txt");
                string linha = sr.ReadLine();

                while (linha != null)
                {
                    acsc.Add(linha);
                    linha = sr.ReadLine();
                }

                sr.Close();
                sr.Dispose();
            }
            catch { }

        }
        //Função que valida se existe o arquivo com as magias
        private void validaListaMagias()
        {
            bool existe = File.Exists(appPath + "\\lista_magias.txt");

            if (!existe)
            {
                if (MessageBox.Show("A lista das spells não existe, vou cria-lo e em seguida você deve re-abrir o "+Application.ProductName+", OK?", "Pergunta:", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    StreamWriter sw = new StreamWriter(appPath + "\\lista_magias.txt");
                    for (int i = 0; i < Funcoes.sMagias.Length; i++)
                    {
                        sw.WriteLine(Funcoes.sMagias[i].ToString());
                    }
                    sw.Flush();
                    sw.Close();

                    Application.Exit();
                }
            }

        }
        //Função carrega lista magias do arquivo e coloca no txt
        private void CarregaListaMagias_txtListaSpells()
        {
            try
            {
                StreamReader sr = new StreamReader(appPath + "\\lista_magias.txt");

                txtListaSpells.Text = sr.ReadToEnd();

                sr.Close();
                sr.Dispose();

            }
            catch { }
        }
        //Função que salva a lista de magias do txt
        private void SalvaListaMagias_txtListaSpells()
        {
            StreamWriter sw = new StreamWriter(appPath + "\\lista_magias.txt");
            sw.Write(txtListaSpells.Text);
            sw.Close();
            sw.Dispose();
        }
        #endregion

        private void btnSalvaListaSpells_Click(object sender, EventArgs e)
        {
            SalvaListaMagias_txtListaSpells();
        }

        private void chkAntiLoggout_Click(object sender, EventArgs e)
        {
            if (chkAntiLoggout.Checked)
            {
                tmrAntiAFK.Start();
            }
            else
            {
                tmrAntiAFK.Stop();
            }
        }

        private void tmrAntiAFK_Tick(object sender, EventArgs e)
        {
            Funcoes.Dance();
        }

        private void chkSpell1_Click(object sender, EventArgs e)
        {
            if (txtSpell1.Text.Length > 2)
            {
                if (chkSpell1.Checked)
                {
                    tmrSpell1.Start();
                }
                else
                {
                    tmrSpell1.Stop();
                }
            }
            else
            {
                chkSpell1.Checked = false;
            }
        }

        private void tmrSpell1_Tick(object sender, EventArgs e)
        {
            if (txtSpell1.Text.Length > 3)
            {
                Funcoes.Say(txtSpell1.Text,1);
            }
        }

        private void chkSpell2_Click(object sender, EventArgs e)
        {
            if (txtSpell2.Text.Length > 2)
            {
                if (chkSpell2.Checked)
                {
                    tmrSpell2.Start();
                }
                else
                {
                    tmrSpell2.Stop();
                }
            }
            else
            {
                chkSpell2.Checked = false;
            }
        }

        private void tmrSpell2_Tick(object sender, EventArgs e)
        {
            if (txtSpell2.Text.Length > 3)
            {
                Funcoes.Say(txtSpell2.Text, 1);
            }
        }

        private void chkFood_Click(object sender, EventArgs e)
        {
            if (cboFood.Text != "" && cboFood.Text != "-- Selecione --")
            {
                if (chkFood.Checked)
                {
                    tmrFood.Start();
                }
                else
                {
                    tmrFood.Stop();
                }
            }
            else
            {
                chkFood.Checked = false;
            }
        }

        private void chkEspecial_Click(object sender, EventArgs e)
        {
            if (txtEspecial.Text.Length > 2)
            {
                if (chkEspecial.Checked)
                {
                    tmrEspecial.Start();
                }
                else
                {
                    tmrEspecial.Stop();
                }
            }
            else
            {
                chkEspecial.Checked = false;
            }
        }

        private void tmrFood_Tick(object sender, EventArgs e)
        {
            Funcoes.Hotkey(cboFood.Text);
        }

        private void tmrEspecial_Tick(object sender, EventArgs e)
        {
            Funcoes.Say(txtEspecial.Text, 1);
        }

        private void chkLight_Click(object sender, EventArgs e)
        {
            if (chkLight.Checked)
            {
                Funcoes.FullLight(true);
            }
            else
            {
                Funcoes.FullLight(false);
            }
        }

        private void tmrInfoChar_Tick(object sender, EventArgs e)
        {
            try
            {
                CultureInfo br = new CultureInfo("pt-BR");
                txtXPChar.Text = Funcoes.GetXP().ToString("n0", br);
                txtXPProximoLevel.Text = Funcoes.CalulaExp().ToString("n0", br);
                txtHPChar.Text = Funcoes.GetHP().ToString();
                txtMPChar.Text = Funcoes.GetMana().ToString();
                txtCapChar.Text = Funcoes.GetCAP().ToString();
                txtLevelChar.Text = Funcoes.LevelChar().ToString();
                txtNomeChar.Text = Funcoes.GetPlayers(16)[0];
                txtSoulChar.Text = Funcoes.PlayerSoul().ToString();
                txtPosicaoChar.Text = Funcoes.ReadInt32(Funcoes.Tibia, Funcoes.Player_X).ToString() + ", " + Funcoes.ReadInt32(Funcoes.Tibia, Funcoes.Player_Y).ToString() + ", " + Funcoes.ReadInt32(Funcoes.Tibia, Funcoes.Player_Z).ToString();
                this.Text = "[" + Funcoes.GetPlayers(16)[0] + "] Tibia Simple Bot";
            }
            catch { }
        }

        private void TrayIcon_DoubleClick(object sender, EventArgs e)
        {
            cmsMostrar_Click(sender, e);
        }

        private void btnBattleListForm_Click(object sender, EventArgs e)
        {
            Form3 frm3 = new Form3();
            frm3.ShowDialog(this);
        }

    }
}
